package com.tang.excel.pa.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.tang.excel.pa.domain.EmQuestionCase;
import com.tang.excel.pa.enums.ProtocolType;
import com.tang.excel.pa.enums.TestType;
import com.tang.excel.pa.excel.RestDoMain;
import com.tang.excel.pa.mapper.EmKnowledgeTestQuestionMapper;
import com.tang.excel.pa.mapper.EmQuestionCaseMapper;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Tang
 * @classname RestDataListener
 * @description [  ]
 * @date 2021/4/25 11:02
 */
public class RestDataListener extends AnalysisEventListener<RestDoMain> {

    private EmKnowledgeTestQuestionMapper emKnowledgeTestQuestionMapper;

    private EmQuestionCaseMapper emQuestionCaseMapper;

    private List<EmQuestionCase> list = new ArrayList<>(10);

    private final Integer offset = 30;

    public RestDataListener(EmKnowledgeTestQuestionMapper emKnowledgeTestQuestionMapper,
                            EmQuestionCaseMapper emQuestionCaseMapper) {
        this.emKnowledgeTestQuestionMapper = emKnowledgeTestQuestionMapper;
        this.emQuestionCaseMapper = emQuestionCaseMapper;
    }

    @Override
    public void invoke(RestDoMain restDoMain, AnalysisContext analysisContext) {
        restDoMain.setTopicId(restDoMain.getTopicId() + offset);
        EmQuestionCase emQuestionCase = new EmQuestionCase();
        BeanUtils.copyProperties(restDoMain, emQuestionCase);
        emQuestionCase.setProtocol(ProtocolType.REST);
        emQuestionCase.setCreateTime(new Date());
        emQuestionCase.setTestType(TestType.getTestTypeMap().get(restDoMain.getTestType()));
        list.add(emQuestionCase);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        emQuestionCaseMapper.batchInsert(list);
    }
}